﻿Imports Microsoft.VisualBasic
Imports System.Reflection

Namespace Secura.Reflection
    Public Class ObjectDebug

        '   ObjectDebug.vb
        '   part of WHMCSdotnet (http://whmcsdotnet.wordpress.com)
        '
        '   Copyright (C) 2009-2011 Mark Castle <markcastle@gmail.com>
        '   All rights reserved. 
        '   Copyright (C) 2009-2011 Secura Hosting Ltd <code@securahosting.com>.
        '   All rights reserved.
        ' 
        '   Redistribution and use in source and binary forms, with or without modification,
        '   are permitted provided that the following conditions are met:
        ' 
        '   - Redistributions of source code must  retain  the  above copyright notice, this
        '     list of conditions and the following disclaimer.
        ' 
        '   - Redistributions in binary form  must  reproduce the  above  copyright  notice,
        '     this list of conditions  and  the  following  disclaimer in  the documentation
        '     and/or other materials provided with the distribution.
        ' 
        '   - Neither  the  name  of  Secura Hosting Ltd, Mark Castle  nor the names of  its
        '     contributors may be used to endorse or  promote  products  derived  from  this
        '     software without specific prior written permission.
        ' 
        '   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
        '   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,  BUT  NOT  LIMITED TO, THE IMPLIED
        '   WARRANTIES  OF  MERCHANTABILITY   AND  FITNESS  FOR  A  PARTICULAR  PURPOSE  ARE
        '   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
        '   ANY DIRECT, INDIRECT, INCIDENTAL,  SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL DAMAGES
        '   (INCLUDING, BUT NOT LIMITED TO,  PROCUREMENT  OF  SUBSTITUTE  GOODS OR SERVICES;
        '   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)  HOWEVER  CAUSED AND ON
        '   ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT  LIABILITY,  OR TORT
        '   (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING  IN  ANY WAY OUT OF THE USE OF THIS
        '   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

        ' This code is derived from: http://www.ogre3d.org/addonforums/viewtopic.php?f=1&t=340
        ' Thank You :-)
        ' Example Usage: 
        ' Dim MyCamera As Camera = MySceneManager.CreateCamera("MainCam")
        ' ReportProperties(MyCamera)

        Public Shared Function ReportProperties(ByVal pObject As Object) As String

            Dim Output As String = ""

            'This subroutine uses reflection to interogate the properties
            'of this class...

            Dim PInfo As PropertyInfo
            Dim Spaces As Integer
            Dim MaxProperyNameLength As Integer

            Output &= "--- Property Report for " & pObject.GetType.ToString & " ---<br/>" & vbCrLf

            'Determine the longest property name (for formatting purposes)...
            For Each PInfo In pObject.GetType.GetProperties()
                If PInfo.Name.Length > MaxProperyNameLength Then
                    MaxProperyNameLength = PInfo.Name.Length
                End If
            Next PInfo

            'By this stage, we know the length (in chars) of the 
            'longest property name.

            For Each PInfo In pObject.GetType.GetProperties()
                Try
                    'Determine the details for this property..
                    Spaces = (MaxProperyNameLength - PInfo.Name.Length) + 1
                    Output &= "Property " & PInfo.Name & Space(Spaces) & " Value: " & PInfo.GetValue(pObject, Nothing).ToString & "<br/>" & vbCrLf
                Catch ex As Exception
                    Output &= "Property " & PInfo.Name & Space(Spaces) & " Value: Unknown<br/>" & vbCrLf
                End Try
            Next PInfo
            Output &= "--- End of Property Report for " & pObject.GetType.ToString & " ---<br/>" & vbCrLf
            Return Output
        End Function     'ReportProperties
    End Class
End Namespace